package zxq.rollcall.course.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import zxq.rollcall.course.controller.action.IGradeController;
import zxq.rollcall.course.entity.SystemGradeTableEntity;
import zxq.rollcall.course.entity.SystemRoleTableEntity;
import zxq.rollcall.course.entity.dto.ResultDTO;
import zxq.rollcall.course.entity.dto.set.GradeDTO;
import zxq.rollcall.course.entity.dto.set.RoleDTO;
import zxq.rollcall.course.entity.query.GradeQuery;
import zxq.rollcall.course.entity.query.PageQuery;
import zxq.rollcall.course.service.MPSystemGradeTableService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系别 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Controller
@RequestMapping("/course/api/grade")
@Api(tags = "系统年级班")
public class SystemGradeTableAction implements IGradeController {
    @Autowired
    MPSystemGradeTableService mpSystemGradeTableService;

    @Override
    public ResultDTO<Boolean> save(GradeQuery gradeQuery) {
        SystemGradeTableEntity systemGradeTableEntity = new SystemGradeTableEntity();
        BeanUtils.copyProperties(gradeQuery,systemGradeTableEntity);
        Boolean rst = mpSystemGradeTableService.save(systemGradeTableEntity);
        if(rst){
            return new ResultDTO<>(1,"插入成功",true);
        }
        return new ResultDTO<>(0,"插入失败",false);
    }

    @Override
    public ResultDTO<Boolean> update(Integer id, GradeQuery gradeQuery) {
        SystemGradeTableEntity systemGradeTableEntity = new SystemGradeTableEntity();
        BeanUtils.copyProperties(gradeQuery,systemGradeTableEntity);
        systemGradeTableEntity.setId(id);
        Boolean rst = mpSystemGradeTableService.updateById(systemGradeTableEntity);
        if(rst) {
            return new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }

    @Override
    public ResultDTO<Boolean> delete(Integer id) {
        Boolean rst = mpSystemGradeTableService.removeById(id);
        if(rst) {
            return new ResultDTO<>(1,"删除成功",true);
        }
        return new ResultDTO<>(0,"删除失败",false);
    }

    @Override
    public ResultDTO<List<GradeDTO>> query(PageQuery pageQuery) {
        Page<SystemGradeTableEntity> page = new Page<>(pageQuery.getPage(),pageQuery.getCount());
        IPage<SystemGradeTableEntity> iPage = mpSystemGradeTableService.page(page);
        List<GradeDTO> roleDTOS = iPage.getRecords().stream().
                map(item -> {
                  GradeDTO gradeDTO = new GradeDTO();
                  BeanUtils.copyProperties(item,gradeDTO);
                  return gradeDTO;
                }).collect(Collectors.toList());
        return new ResultDTO<>(1,"查询成功",roleDTOS);
    }

}

